var designClass = "design_class", hideClass = "hide_class", browseClass = "browse_class", addClass = "add_class", editClass = "edit_class";

var upWindow = window.parent.frames["zwIframe"];
var designClass = "design_class", hideClass = "hide_class", browseClass = "browse_class", addClass = "add_class", editClass = "edit_class";

function fy_dialog_close(){
	window.parent.$(".mask").remove();
	window.parent.parent.$(".layui-layer-iframe").parent().find(".layui-layer-close").trigger("click");
}
	function getUpFieldsByFieldval(name) {
		return upWindow.$("[fieldval*='displayName:\"" + name + "\"']");
	}

	function getUpFieldValue(field) {
		var fieldVal = field.attr("fieldVal");
		var editTag = field.hasClass(editClass);
		var browseTag = field.hasClass(browseClass);
		var addTag = field.hasClass(addClass);
		if (fieldVal != null && typeof (fieldVal) != 'undefined') {
			fieldVal = eval("(" + fieldVal + ")");
		} else {
			return null;
		}
		var obj = new Object();
		obj.displayName = fieldVal.displayName;
		obj.name = fieldVal.name;
		var tag = true;
		switch (fieldVal.inputType) {
		case "text":
		case "textarea":
			if (editTag) {
				obj.value = field.find("#" + fieldVal.name).val();
			} else if (browseTag) {
				obj.value = fieldVal.value;
			} else if (addTag) {
				obj.value = field.find("#" + fieldVal.name).val();
			}
			break;
		default:
			tag = false;
		}
		if (obj.value != undefined) {
			obj.value = obj.value.replace("\r\n", ";");
			obj.value = obj.value.replace("\n", ";");
		}
		return obj;
	}

	function setUpFieldValue2(field, value, displayValue, index) {
			var fieldVal = field.attr("fieldVal");
			var editTag = field.hasClass(editClass);
			var browseTag = field.hasClass(browseClass);
			var addTag = field.hasClass(addClass);
			if (fieldVal != null && typeof (fieldVal) != 'undefined') {
				fieldVal = eval("(" + fieldVal + ")");
			} else {
				return null;
			}
			var tag = true;
			switch (fieldVal.inputType) {
			case "text":
			case "textarea":
				if (editTag) {
					field.find("#" + fieldVal.name).val(value);
					field.find("#" + fieldVal.name).trigger("blur");
				} else if (addTag) {
					field.find("#" + fieldVal.name).val(
							field.find("#" + fieldVal.name).val() + value);
					field.find("#" + fieldVal.name).trigger("blur");
				}
				break;
			case "date":
				if (editTag) {
					field.find("#" + fieldVal.name).val(value);
					field.find("#" + fieldVal.name).trigger("blur");
				}
				break;
			}
	}

	var codes = [];

	var xmlObj = null;

	var needDate = false;
	var newAway = false;

	var codeInputArray = getUpFieldsByFieldval("出差人员工号");
	var nameInputArray = getUpFieldsByFieldval("姓名1");
	var depInputArray = getUpFieldsByFieldval("出差人员所在部门1");
	var postInputArray = getUpFieldsByFieldval("出差人员岗位1");
	var dateInputArray = getUpFieldsByFieldval("入厂时间1");
	var bg23InputArray = getUpFieldsByFieldval("银行卡号");
	var idInputArray = getUpFieldsByFieldval("身份证号");
	var mobileInputArray = getUpFieldsByFieldval("联系方式");
	if (dateInputArray.length > 0) {
	
		needDate = true;
		
	}

	if(mobileInputArray.length > 0){
		newAway=true;
	}
	function init() {
	
		for ( var i = 0, length = codeInputArray.length; i < length; i++) {

			codes.push(getUpFieldValue(codeInputArray.eq(i)).value);
			setUpFieldValue2(nameInputArray.eq(i), "");
			setUpFieldValue2(depInputArray.eq(i), "");
			setUpFieldValue2(postInputArray.eq(i), "");

  	
			  if (needDate) {
			
		            setUpFieldValue2(dateInputArray.eq(i), "");
		        }
			if(newAway){
			
		        	   setUpFieldValue2(bg23InputArray.eq(i), "");
		               setUpFieldValue2(idInputArray.eq(i), "");
		               setUpFieldValue2(mobileInputArray.eq(i), "");
				} 
		
		}
	}

	function load() {

		if (window.XMLHttpRequest) {
			xmlObj = new XMLHttpRequest();
		} else {
			if (window.ActiveXObject) {
				xmlObj = new ActiveXObject("Microsoft.XMLHTTP");
			} else {
				return;
			}
		}
		xmlObj.onreadystatechange = handleResponse;
		xmlObj.open("get",
				"./oa2hrController.do?method=findPersonInfo&codes="
						+ codes.join(",") + "&needDate=" + needDate+ "&newAway=" + newAway, true);
		xmlObj.send();

	}
	function handleResponse() {
		if (xmlObj.readyState == 4) {//xmlObj loaded
			var success = false;
			if (xmlObj.status == 200) {
				var datas = eval("[" + xmlObj.responseText + "]");
				if (datas.length == 1) {
					var personMap = datas[0];
					var notExistPk_tbm_psndocUser = [];
					var pk_corp = 0;
					for ( var i = 0, length = codeInputArray.length; i < length; i++) {
						var person = personMap["code"
								+ getUpFieldValue(codeInputArray.eq(i)).value];
						if (person) {
							if (pk_corp == 0) {
								pk_corp = person["pk_corp"];
							} else {
								if (pk_corp != person["pk_corp"]) {
									alert("同一张出差单的出差人员不能分属不同制造部,请分开填写。谢谢！");
									break;
								}
							}
							/**
							nameInputArray[i].value = person["name"];
							depInputArray[i].value = person["dep"];
							postInputArray[i].value = person["post"];
							if (needDate) {
								dateInputArray[i].value = person["date"];
							}
							 **/
							setUpFieldValue2(nameInputArray.eq(i),
									person["name"]);
							setUpFieldValue2(depInputArray.eq(i),
									person["dep"]);
							setUpFieldValue2(postInputArray.eq(i),
									person["post"]);

							
						       if (needDate){
		                            setUpFieldValue2(dateInputArray.eq(i),
		                                    person["date"]);
		                        }
						     if(newAway){
		                        	   setUpFieldValue2(bg23InputArray.eq(i),
		                                       person["bgroupdef23"]);
		                        	   setUpFieldValue2(idInputArray.eq(i),
		                                       person["id"]);
		                        	   setUpFieldValue2(mobileInputArray.eq(i),
		                                       person["mobile"]);
		                        } 

							if (person["existPk_tbm_psndoc"] == false) {
								notExistPk_tbm_psndocUser
										.push(person["name"]);
							}
						}
					}
					success = true;
					if (notExistPk_tbm_psndocUser.length != 0) {
						alert(notExistPk_tbm_psndocUser.join("、")
								+ "在ＨＲ中不存在考勤档案，\n无法自动将出差数据录入ＨＲ");
					}
				}
			}
			if (!success) {
				alert("请检查输入的工号");
			}
			fy_dialog_close();
		}

	}
	init();
	load();


